leetcode/100-n/523. 连续的子数组和.md
https://leetcode-cn.com/problems/continuous-subarray-sum/
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
int numLen = nums.length;
if(numLen < 2){
return false;
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//用来计算为0的结果,-1表示最短为2才能返回true
map.put(0, -1);
int numsPreSum = 0;
for(int i = 0; i < numLen; i++){
numsPreSum = (numsPreSum + nums[i]) % k;
if(map.containsKey(numsPreSum)){
//距离大于等于2 由于是前缀和,所以要排除首位
if(map.get(numsPreSum) + 2 <= i){
return true;
}
}else{
map.put(numsPreSum, i);
}
}
return false;
}
}